<?php

class Password extends BasePassword {
	private $key = null;

	public function setToken($token) {
		$this->key = hash(sfConfig::get('app_crypto_hash'), sfConfig::get('app_secret_key') . substr($token, 0, 12), true);
	}

	public function setValue($value) {
		if($this->key===null) return $this;
		$iv_size = intval(sfConfig::get('app_crypto_iv_size'));
		if( $iv_size > 0) {
			$this->setIv(mcrypt_create_iv($iv_size, MCRYPT_RAND));
		}
		return $this->_set('value', base64_encode(mcrypt_encrypt(sfConfig::get('app_crypto_cipher'), $this->key, $value, sfConfig::get('app_crypto_mode'), $this->iv)));
	}

	public function getValue() {
		if($this->key===null) return false;
		return str_replace("\0", '', mcrypt_decrypt(sfConfig::get('app_crypto_cipher'), $this->key, base64_decode($this->_get('value')), sfConfig::get('app_crypto_mode'), $this->iv));
	}

	public function setIv($iv) {
		return $this->_set('iv', base64_encode($iv));
	}

	public function getIv() {
		return base64_decode($this->_get('iv'));
	}
}
